-- @owner: @zhanghuan96
-- @date: 2024/12/12
-- @testpoint: gms_UTILITY.FORMAT_CALL_STACK多次调用
@conn PrimaryDbAmode;
--前置条件
drop extension if exists gms_utility;
create extension gms_utility;
--step1:创建表;expect:成功
drop TABLE if exists tb_format0007;
CREATE TABLE tb_format0007 (
    log_id      serial,
    log_time    TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    call_stack  CLOB
);
--step2:多次调用函数;expect:成功
DECLARE
    v_call_stack1 CLOB;
    v_call_stack2 CLOB;
    v_call_stack3 CLOB;
    v_call_stack4 CLOB;
BEGIN
     v_call_stack1 := gms_UTILITY.FORMAT_CALL_STACK;
     v_call_stack2 := gms_UTILITY.FORMAT_CALL_STACK;
     v_call_stack3 := gms_UTILITY.FORMAT_CALL_STACK;
     v_call_stack4 := gms_UTILITY.FORMAT_CALL_STACK;
    INSERT INTO tb_format0007 (call_stack) VALUES (v_call_stack1);
    INSERT INTO tb_format0007 (call_stack) VALUES (v_call_stack2);
    INSERT INTO tb_format0007 (call_stack) VALUES (v_call_stack3);
    INSERT INTO tb_format0007 (call_stack) VALUES (v_call_stack4);
END;
/
--step3:查询表;expect:call_stack列插入函数调用结果信息
select * from tb_format0007;
--step4:创建存储过程;expect:成功
drop function if exists func_format0007_01;
create or replace function func_format0007_01
returns void as $$
begin
  RAISE INFO 'INFO:func_format0004_01 call stack: ';
  RAISE INFO 'INFO:%', gms_utility.format_call_stack();
end;
$$ language plpgsql;
/
drop procedure if exists proc_format0007;
create or replace procedure proc_format0007_02 (count in out int, max in int)
as
begin
 if count < max then
 count := count + 1;
 proc_format0007_02(count, max);
 else
 func_format0007_01();
 end if;
end;
/
--step5:执行存储过程;expect:成功
declare
 count int := 0;
begin
 proc_format0007_02(count, 5);
end;
/
declare
 count int := 0;
begin
 proc_format0007_02(count, 20);
end;
/
--step6:清理环境;expect:成功
drop TABLE if exists tb_format0007;
drop function if exists func_format0007_01;
drop procedure if exists proc_format0007_02 ;
drop extension if exists gms_utility;